//
//  AddPhoneVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/3/22.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "AddPhoneVC.h"

#import "UserfulOneCell.h"
#import "UserfulPhoneTwoCell.h"
#import "UserfulThreeCell.h"
#import "UserfulForeCell.h"
#import "UserfulTwoCell.h"
#import "CellThreeView.h"

#import "TodoHttpManager.h"
#import "FTPopOverMenu.h"
#import "AddMModel.h"
#import "KangYinSuVc.h"

#import "ClientViewController.h"

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

@interface AddPhoneVC ()<UITableViewDelegate,UITableViewDataSource,UserfulForeCellDelegate,UITextFieldDelegate,UserfulThreeCellDelegate,UserfulOneCellDelegate,ABPeoplePickerNavigationControllerDelegate,CellThreeViewDelegate>

{
    NSMutableArray *_titleArr;//左边的标题
    NSMutableArray *_shuiYinArr;
    
    NSMutableArray *tfArr;
    NSMutableArray *oneArr;
    NSMutableDictionary *zongDic;
    NSMutableArray *egeArr;
    NSMutableArray *egeJArr;
    NSMutableArray *dengArr;
    NSMutableArray *dengJArr;
    NSMutableArray *_oneArr;
    NSMutableArray *_bArr;
    NSMutableArray *_cArr;
    NSMutableArray *_dArr;
    NSMutableArray *_eArr;
    NSMutableArray *_fArr;
    NSMutableArray *_gArr;
    NSMutableArray *_hArr;
    NSMutableArray *_iArr;
    NSMutableArray *_JArr;
    NSMutableArray *_kArr;
    
    NSString *_timeStr;//来电日期
    NSString *_sexStr;//性别

    NSString *_keDengStr;//客户等级
 
    NSString *_adStr;
    NSString *_tQStr;
    NSString *_tFStr;
    
    NSString *_xWStr;
    NSString *_xMianStr;
    NSString *_xHuStr;
    NSString *_poneStr;
    NSString *_xiaStr;
   
    
    
    ABPeoplePickerNavigationController *_abPeoplePickerVc;
    UIButton *_btn1;
    UIButton *_btn2;
    UIButton *_btn3;
    NSMutableArray *_exitArr;
    
    UserfulTwoCell *_threecell;
    
    BOOL _isupdatelayout;
    
    CGFloat changeheight;


}


@property (nonatomic, strong) NSMutableArray         *seletedPhoneArry;
@property (nonatomic, assign, getter = isSelf_adaption) BOOL self_adaption;

@property (nonatomic, strong) NSString         *_is_string;

@property (nonatomic, strong) CellThreeView         *threeview;





@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableArray *keArr;
@property(nonatomic,strong)NSMutableArray *keXArr;
@property(nonatomic,strong)NSMutableArray *adArr;
@property(nonatomic,strong)NSMutableArray *adXArr;
@property(nonatomic,strong)NSMutableArray *tuiQArr;
@property(nonatomic,strong)NSMutableArray *tuiQXArr;
@property(nonatomic,strong)NSMutableArray *tuiFArr;
@property(nonatomic,strong)NSMutableArray *tuiFXArr;
@property(nonatomic,strong)NSMutableArray *xuWArr;
@property(nonatomic,strong)NSMutableArray *xuWXArr;
@property(nonatomic,strong)NSMutableArray *xuMianArr;
@property(nonatomic,strong)NSMutableArray *xuMianXArr;
@property(nonatomic,strong)NSMutableArray *xuHuArr;
@property(nonatomic,strong)NSMutableArray *xuHuXArr;
@property(nonatomic,strong)NSMutableDictionary *phoneDic;
@property(nonatomic,strong)AddMModel *model;

@property (nonatomic, strong) NSMutableArray         *pphoneArray;
//男女选择
@property (nonatomic,strong) NSString *sexString;

@end

@implementation AddPhoneVC


- (NSMutableArray *)seletedPhoneArry{
    if (!_seletedPhoneArry) {
        _seletedPhoneArry = [NSMutableArray array];
    }
    return _seletedPhoneArry;
}
- (CellThreeView *)threeview{
    if (!_threeview)
    {
        CGFloat vHeight = 0;
        if (self.seletedPhoneArry.count>=3) {
            vHeight = 150;
        }else if (self.seletedPhoneArry.count==2){
            vHeight = 100;
        }else{
            vHeight = 50;
        }
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.seletedPhoneArry];
        _threeview = [[CellThreeView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, vHeight) witharray:arr];
        _threeview.backgroundColor = [UIColor whiteColor];
        _threeview.dataarray = arr;
        _isupdatelayout = NO;
        __weak __typeof(self) wself = self;
        
        _threeview.rerturnbtn = ^(CGFloat height) {
            _isupdatelayout = YES;
            changeheight = height;
            [wself changethreecellframewithheight:height];
            [wself.tabView reloadData];
        };
    }
    
    return _threeview;
}

- (void)changethreecellframewithheight:(CGFloat )height{
    CGRect frame = _threecell.frame;
    frame.size.height = height;
    _threecell.frame = frame;
    
}




-(void)viewWillAppear:(BOOL)animated
{
    //下拉菜单
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuDropDown:) name:TDGuDropDown object:nil];
   
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDGuPhoneZeng:) name:TDGuPhoneZeng object:nil];
    
      self.tabBarController.tabBar.hidden  = YES;
}
-(void)didTDGuDropDown:(NSNotification *)noti
{
    if (noti.object != 0)
    {
        _phoneDic = [noti.object objectForKey:@"data"];
        //客户等级
        _keArr = [_phoneDic objectForKey:@"khdj"];
        for (int i = 0; i < _keArr.count; i++)
        {
            NSDictionary *dict = [_keArr objectAtIndex:i];
            [_keXArr addObject:dict[@"content"]];
        }
        
        //广告推广
        _adArr = [_phoneDic objectForKey:@"ggtg"];
    
        for (int i = 0; i < _adArr.count; i++)
        {
            NSDictionary *dict = [_adArr objectAtIndex:i];
         
            [_adXArr addObject:dict[@"content"]];
         
        }
        
        //推介渠道
        _tuiQArr = [_phoneDic objectForKey:@"tjqd"];
        for (int i = 0; i < _tuiQArr.count; i++)
        {
            NSDictionary *dict = [_tuiQArr objectAtIndex:i];
            [_tuiQXArr addObject:dict[@"content"]];
        }
        //推介方式
        _tuiFArr = [_phoneDic objectForKey:@"tjfs"];
        for (int i = 0; i < _tuiFArr.count; i++)
        {
            NSDictionary *dict = [_tuiFArr objectAtIndex:i];
            [_tuiFXArr addObject:dict[@"content"]];
        }
        //需求物业
        _xuWArr = [_phoneDic objectForKey:@"need_property"];
        for (int i = 0; i < _xuWArr.count; i++)
        {
            NSDictionary *dict = [_xuWArr objectAtIndex:i];
            [_xuWXArr addObject:dict[@"content"]];
        }
        //需求面积
        _xuMianArr = [_phoneDic objectForKey:@"order_area"];
        for (int i = 0; i < _xuMianArr.count; i++)
        {
            NSDictionary *dict = [_xuMianArr objectAtIndex:i];
            [_xuMianXArr addObject:dict[@"content"]];
        }
        //需求户型
        _xuHuArr = [_phoneDic objectForKey:@"order_type"];
        for (int i = 0; i < _xuHuArr.count; i++)
        {
            NSDictionary *dict = [_xuHuArr objectAtIndex:i];
            [_xuHuXArr addObject:dict[@"content"]];
        }
        
        
        
        
        
        [_tabView reloadData];
    }
    
}

-(void)didTDGuPhoneZeng:(NSNotification *)notifica
{
    if (notifica.object != nil)
    {
        int statue = [[notifica.object objectForKey:@"status"] intValue];
           NSString *info = [notifica.object objectForKey:@"info"];
        if (statue == 1)
        {
            NSLog(@"保存成功");
            [_manager MakeMyselfAlert:info andIsNeedTitle:NO andShowTime:2.0];
             //ClientViewController *visitVc = [[ClientViewController alloc] init];
           [self.navigationController popToRootViewControllerAnimated:YES];
            [_tabView reloadData];
            
        }else
        {
            [_manager MakeMyselfAlert:info andIsNeedTitle:NO andShowTime:2.0];
            

        }
        
    }

    
    
    
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDGuDropDown object:nil];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新增来电";
      _model = [[AddMModel alloc]init];
    _titleArr = [NSMutableArray arrayWithObjects:@"来电时间:",@"客户姓名:",@"联系方式:",@"客户性别:",@"客户等级:",@"广告推广:",@"推广渠道:",@"推介方式:",@"推荐人姓名:",@"推荐人电话:",@"需求物业:",@"需需求面积:",@"需求户型:",@"电话记录:",@"下次跟进日期:", nil];
    
    
    _shuiYinArr = [NSMutableArray arrayWithObjects:@"",@"请输入姓名:",@"请输入联系电话:",@"",@"",@"",@"",@"",@"请输入推荐人姓名",@"请输入推荐人电话",@"",@"",@"",@"请输入电话记录:",@"", nil];
   
    
    _keArr = [NSMutableArray array];
    _keXArr = [NSMutableArray array];
    _adArr = [NSMutableArray array];
    _adXArr = [NSMutableArray array];
    _tuiQArr = [NSMutableArray array];
    _tuiQXArr = [NSMutableArray array];
    _tuiFArr = [NSMutableArray array];
    _tuiFXArr = [NSMutableArray array];
    _xuWArr = [NSMutableArray array];
    _xuWXArr = [NSMutableArray array];
    _xuMianArr = [NSMutableArray array];
    _xuMianXArr = [NSMutableArray array];
    _xuHuArr = [NSMutableArray array];
    _xuHuXArr = [NSMutableArray array];
    _pphoneArray = [NSMutableArray array];
    _phoneDic = [NSMutableDictionary dictionary];
    
    _model = [[AddMModel alloc] init];
    _manager = [TodoHttpManager sharedManager];
     [_manager userGuWenDropDown];
    
    [self.seletedPhoneArry addObject:@""];
    self.pphoneArray = [NSMutableArray new];

    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStyleGrouped];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 50;
    [_tabView registerClass:[UserfulForeCell class] forCellReuseIdentifier:@"CellE"];
    [_tabView registerClass:[UserfulOneCell class] forCellReuseIdentifier:@"CellA"];
    [_tabView registerClass:[UserfulPhoneTwoCell class] forCellReuseIdentifier:@"CellB"];
    [_tabView registerClass:[UserfulThreeCell class] forCellReuseIdentifier:@"CellC"];
   
    [self.view addSubview:_tabView];

    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArr.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame= CGRectMake(20, 25,B_WIDTH-20*2, 45);
    btn.layer.cornerRadius = 6;
    btn.backgroundColor = [UIColor orangeColor];
    [btn setTitle:@"保存" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn addTarget:self action:@selector(visitBaoCunBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    [self.view addSubview:view];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row == 0||indexPath.row == 14)
    {
        
        //带日历
        NSString *CellIdentifier1 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        UserfulOneCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil)
        {
            cell = [[UserfulOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
        }
        if (indexPath.row == 0)
        {
//            //获取当前时间日期
//            NSDate *date=[NSDate date];
//            NSDateFormatter *format1=[[NSDateFormatter alloc] init];
//            [format1 setDateFormat:@"yyyy-MM-dd"];
//            //NSString *dateStr;
//            _timeStr=[format1 stringFromDate:date];

            cell.timeLab.text = _timeStr;
        }
        if (indexPath.row == 14)
        {
                 cell.timeLab.text = _xiaStr;
        }
        cell.delegate = self;
        cell.riLBtn.tag = indexPath.row;
   
        cell.firstLab.text = _titleArr[indexPath.row];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
    if (indexPath.row ==1||indexPath.row==2||indexPath.row==8||indexPath.row==9||indexPath.row==13)
    {
        //TextField
//        NSString *CellIdentifier2 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
//        
//        UserfulPhoneTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
//        if (cell == nil)
//        {
//            cell = [[UserfulPhoneTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
//        }
//        
//        cell.textViewBlock = ^(){
//            
//            NSLog(@"123");
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
//            
//            //将要编辑的textfield 所在的cell  使其滚动到顶部的位置
//            
//            [_tabView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
//            
//        };
//        [cell setPhoneheadStr:nil andDetail:nil andModel:_model];
//       // [cell setheadStr:nil andDetail:nil andModel:_model];
//        cell.oneTF.delegate = self;
//        cell.oneTF.tag = indexPath.row;
//        cell.firstLab.text = _titleArr[indexPath.row];
//        cell.oneTF.placeholder = _shuiYinArr[indexPath.row];
//        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//        return cell;
        //TextField
        NSString *CellIdentifier2 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        UserfulTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        if (cell == nil)
        {
            cell = [[UserfulTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
            if (indexPath.row==1) {

                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.layer.cornerRadius = 6;
                // btn.backgroundColor = [UIColor orangeColor];
                [btn setImage:[UIImage imageNamed:@"Btn_daoRu"] forState:0];
                [btn addTarget:self action:@selector(gotoAddressBookBtnClick1:) forControlEvents:UIControlEventTouchUpInside];
                btn.tag = 1;
                [cell addSubview:btn];
                _btn1 = btn;
                _btn1.selected = NO;
                [_exitArr addObject:_btn1];
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.mas_equalTo(CGSizeMake(30, 30));
                    make.right.equalTo(cell.contentView).offset(0);
                    make.top.equalTo(cell.mas_top).offset(10);
                }];
                cell.tag = 1001;
                
            }
            if (indexPath.row==2) {
                cell.backgroundColor = [UIColor yellowColor];
                _threecell = cell;
                
                
                [cell.oneTF removeFromSuperview];
                [cell.firstLab removeFromSuperview];
                [cell.contentView addSubview:self.threeview];
                self.threeview.delegate = self;
            }
        }
        cell.textViewBlock = ^(){
            
            NSLog(@"123");
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
            
            //将要编辑的textfield 所在的cell  使其滚动到顶部的位置
            
            [_tabView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
            
        };
        
        cell.oneTF.delegate = self;
        cell.oneTF.tag = indexPath.row;
        [cell setPhoneheadStr:nil andDetail:nil andModel:_model];
        cell.firstLab.text = _titleArr[indexPath.row];
        cell.oneTF.placeholder = _shuiYinArr[indexPath.row];

        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        if (indexPath.row==1) {
            
            if (_model.xingming.length>0) {
                cell.oneTF.text = _model.xingming;
            }

            
        }
        
        if (indexPath.row==2) {
            cell.oneTF.text = _model.fangshi;
        }
        return cell;
  
    }
    if (indexPath.row == 3)
        
    {
        //男女选择
        NSString *CellIdentifier3 =@"CELL";
        UserfulThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
        if (cell == nil)
        {
            cell = [[UserfulThreeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3];
        }
        
        cell.delegate = self;
        cell.firstLab.text = _titleArr[indexPath.row];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        return cell;
    }
       else
    {
        //下拉
        NSString *CellIdentifier4 = [NSString stringWithFormat:@"Cell%ld%ld", (long)[indexPath section], (long)[indexPath row]];
        UserfulForeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier4];
        if (cell == nil)
        {
            cell = [[UserfulForeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier4];
        }
        cell.delegate = self;
        cell.firstLab.text = _titleArr[indexPath.row];
        cell.selectBtn.tag = indexPath.row;
        cell.delegate = self;
        
        [cell.selectBtn addTarget:self action:@selector(visitSelectedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
         return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.view endEditing:YES];
    
}

// type 1+2-
-(void)cellThreeViewBtnClickWithIndex:(NSInteger)index Type:(NSInteger)type{
    if (index==1) { // 1
        if (type==1) {//+
            [self.seletedPhoneArry replaceObjectAtIndex:0 withObject:_threeview.tf1.text];
            if (_threeview.tf2.text.length>0) {
                [self.seletedPhoneArry replaceObjectAtIndex:1 withObject:_threeview.tf2.text];
                [self.seletedPhoneArry addObject:@""];
            }else {
                [self.seletedPhoneArry addObject:@""];
            }
            
        }else{//-
            if (_threeview.tf3.text.length>0) {
                [self.seletedPhoneArry replaceObjectAtIndex:2 withObject:_threeview.tf3.text];
            }
            [self.seletedPhoneArry removeObjectAtIndex:0];
        }
    }else if (index==2){ // 2
        if (_threeview.tf3.text.length>0) {
            [self.seletedPhoneArry replaceObjectAtIndex:2 withObject:_threeview.tf3.text];
        }
        [self.seletedPhoneArry removeObjectAtIndex:1];
        
    }else{ // 3
        [self.seletedPhoneArry removeObjectAtIndex:2];
        
    }
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.seletedPhoneArry];
    [self.threeview reloadviewwitharray:arr];
    
    //    [self.threeview reloadviewwitharray:arr];
    [self.tabView reloadData];
    self.pphoneArray = [NSMutableArray arrayWithArray:self.seletedPhoneArry];
}

#pragma mark - 改变tableview的位置（当将要编辑的时候）
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
    
    //将要编辑的textfield 所在的cell  使其滚动到中间的位置
    
    [_tabView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    return YES;
}


//键盘收回触发
- (void)textFieldDidEndEditing:(UITextField *)textField{
    //回归原处
    //    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    // NSLog(@"----000   --  %@",textField.text);
    
    if (textField.tag == 1) {
        _model.xingming = textField.text;
    }else if (textField.tag == 2){
        _model.fangshi = textField.text;
    }else if (textField.tag == 8){
        _model.tuiming = textField.text;
    }else if (textField.tag == 9){
        _model.tuidianhua = textField.text;
    }else if (textField.tag == 13){
        _model.baimiao = textField.text;
    
    }
    else
    {
    }
    
}

#pragma  mark-- cell的代理方法
-(void)changeImageBtnClick:(UIButton *)sender
{
    
    if (sender.tag==50) {
        _sexString = @"1";
        
    }else{
        _sexString = @"2";
        
    }
}



//日历
-(void)riLiBtnClick:(UIButton *)sender withLab:(NSString *)riLiStr
{
    if (sender.tag == 0)
    {
        _timeStr = riLiStr;
        NSLog(@"00000000=====%@",_timeStr);
    }
    if (sender.tag == 14)
    {
        _xiaStr = riLiStr;
        NSLog(@"24242424242424 ---T ==%@",_xiaStr);
    }
}

-(void)visitSelectedBtnClick:(UIButton *)sender
{
    // NSLog(@"daijjideanniu =======%ld",sender.tag);
    if (sender.tag == 4)
    {
        //客户等级
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_keXArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_keArr[selectedIndex][@"content"] forState:UIControlStateNormal];
             
             _keDengStr = _keArr[selectedIndex][@"parameter_id"];
             
         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 5)
    {
        
        
            //广告推广
            [FTPopOverMenu showForSender:sender
                           withMenuArray:_adXArr
                              imageArray:nil
                               doneBlock:^(NSInteger selectedIndex)
             {
                 [sender setTitle:_adArr[selectedIndex][@"content"] forState:UIControlStateNormal];
                 
                 _adStr = _adArr[selectedIndex][@"parameter_id"];
                 if (![_adStr isEqualToString:@"无"] && ![_adStr isEqualToString:@"0"]) {
                     _tQStr = @"";
                     UserfulForeCell *cell = (UserfulForeCell *)[self.tabView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:6 inSection:0]];
                     [cell.selectBtn setTitle:@"无" forState:UIControlStateNormal];
                 }
//                 int AA = 6;
//                 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:AA inSection:0];
//                 [_tabView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
             }
                            dismissBlock:^
             {
                 
             }];

    }
    else if (sender.tag == 6)
    {
            
            
            [FTPopOverMenu showForSender:sender
                           withMenuArray:_tuiQXArr
                              imageArray:nil
                               doneBlock:^(NSInteger selectedIndex)
             {
                 [sender setTitle:_tuiQArr[selectedIndex][@"content"] forState:UIControlStateNormal];
                 
                 _tQStr = _tuiQArr[selectedIndex][@"parameter_id"];
                 if (![_tQStr isEqualToString:@"无"] && ![_tQStr isEqualToString:@"0"]) {
                     _adStr = @"";
                     UserfulForeCell *cell = (UserfulForeCell *)[self.tabView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:0]];
                     [cell.selectBtn setTitle:@"无" forState:UIControlStateNormal];
                 }
                 
//                 int AA = 5;
//                 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:AA inSection:0];
//                 [_tabView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
             }
                            dismissBlock:^
             {
                 
             }];
   
    }
    else if (sender.tag == 7)
    {
        //推介方式
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_tuiFXArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_tuiFArr[selectedIndex][@"content"] forState:UIControlStateNormal];
         
             _tFStr = _tuiFArr[selectedIndex][@"parameter_id"];
         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 10)
    {
        //需求物业
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_xuWXArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_xuWArr[selectedIndex][@"content"] forState:UIControlStateNormal];
        
             _xWStr = _xuWArr[selectedIndex][@"parameter_id"];
         }
                        dismissBlock:^
         {
             
         }];
    }
    else if (sender.tag == 11)
    {
        //需求面积
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_xuMianXArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_xuMianArr[selectedIndex][@"content"] forState:UIControlStateNormal];
            
             _xMianStr = _xuMianArr[selectedIndex][@"parameter_id"];
         }
                        dismissBlock:^
         {
             
         }];
    }
    
    else
    {
        //需求户型
        
        [FTPopOverMenu showForSender:sender
                       withMenuArray:_xuHuXArr
                          imageArray:nil
                           doneBlock:^(NSInteger selectedIndex)
         {
             [sender setTitle:_xuHuArr[selectedIndex][@"content"] forState:UIControlStateNormal];
          
             _xHuStr = _xuHuArr[selectedIndex][@"parameter_id"];
         }
                        dismissBlock:^
         {
             
         }];
        
        
    }
    
    
    
}
-(void)visitBaoCunBtnClick:(UIButton *)btn
{
    if (!_sexString) {
        _sexString = @"1";
    }
    
    NSUserDefaults *userDefa = [NSUserDefaults standardUserDefaults];
    
    NSString *uid= [userDefa objectForKey:@"USER_ID"];
    
//    NSLog(@"1111111--------%@",_timeStr);
//     NSLog(@"1111111--------%@",_model.xingming);
//     NSLog(@"1111111--------%@",_model.fangshi);
//     NSLog(@"1111111--------%@",_sexString);
//     NSLog(@"1111111--------%@",_keDengStr);
//     NSLog(@"1111111--------%@",_adStr);
//    NSLog(@"1111111--------%@",_tQStr);
//     NSLog(@"1111111--------%@",_tFStr);
//     NSLog(@"1111111--------%@",_model.tuiming);
//     NSLog(@"1111111--------%@",_model.tuidianhua);
//     NSLog(@"1111111--------%@",_xWStr);
//    NSLog(@"1111111--------%@",_xMianStr);
//    NSLog(@"1111111--------%@",_xHuStr);
//    NSLog(@"1111111--------%@",_model.baimiao);
//    NSLog(@"1111111--------%@",_xiaStr);
//    
//    NSString *phone1 = nil;
//    NSString *phone2 = nil;
//    NSString *phone3 = nil;
    if (self.pphoneArray.count==0) {
        
    }else if (self.pphoneArray.count==1){
        _threeview.tf1.text = self.pphoneArray[0];
    }else if (self.pphoneArray.count==2){
        _threeview.tf1.text = self.pphoneArray[0];
        _threeview.tf2.text = self.pphoneArray[1];
    }else if (self.pphoneArray.count==3){
        _threeview.tf1.text = self.pphoneArray[0];
        _threeview.tf2.text = self.pphoneArray[1];
        _threeview.tf3.text = self.pphoneArray[2];
        
    }
    
    UserfulTwoCell *cell1 = (UserfulTwoCell *)[self.view viewWithTag:1001];
    
    if (cell1.oneTF.text.length==0) {
        [_manager MakeMyselfAlert:@"请输入客户姓名" andIsNeedTitle:NO andShowTime:2.0];
        return;
    }
    if (_threeview.tf1.text.length==0) {
        [_manager MakeMyselfAlert:@"请输入联系方式" andIsNeedTitle:NO andShowTime:2.0];
        return;
    }
    
    //TODO:新增来电
 
    UserfulPhoneTwoCell *cell = (UserfulPhoneTwoCell *)[self.tabView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:13 inSection:0]];
    _model.baimiao = cell.oneTF.text;
    
    [_manager userGuwenwithName:cell1.oneTF.text withPhone:_threeview.tf1.text withPhone_a:_threeview.tf2.text withPhone_B:_threeview.tf3.text withWuYe:_xWStr withArea:_xMianStr withHouse:_xHuStr withSex:_sexString withPhoneTime:_timeStr  withKeDengJ:_keDengStr withAD:_adStr withTQu:_tQStr withTWay:_tFStr withTName:_model.tuiming withTPhone:_model.tuidianhua withPhoneJ:_model.baimiao withNextTime:_xiaStr withAdviser:uid];
    
}



- (void)gotoAddressBookBtnClick1:(UIButton *)btn{
    [self gotoAddressBookwithtag:btn.tag];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==2) {
        if (self.seletedPhoneArry.count>=3) {
            return 150;
        }else if (self.seletedPhoneArry.count==2){
            return 100;
        }else{
            return 50;
        }
    }else{
        return 50;
    }
    return 50;
}

/**
 2  打开通讯录
 3  */
- (void)gotoAddressBookwithtag:(NSInteger )teger{
    
    _abPeoplePickerVc = [[ABPeoplePickerNavigationController alloc] init];
    _abPeoplePickerVc.peoplePickerDelegate = self;
    
    //下面的判断是ios8之后才需要加的，不然会自动返回app内部
    //         if(IS_iOS8){
    //
    //                 //predicateForSelectionOfPerson默认是true （当你点击某个联系人查看详情的时候会返回app），如果你默认为true 但是实现-peoplePickerNavigationController:didSelectPerson:property:identifier:
    //        　　　　　　  // 代理方法也是可以的，与此同时不能实现peoplePickerNavigationController: didSelectPerson:不然还是会返回app。
    //                 //总之在ios8之后加上此句比较稳妥
    //                 _abPeoplePickerVc.predicateForSelectionOfPerson = [NSPredicate predicateWithValue:true];
    //
    //                 //predicateForSelectionOfProperty默认是true （当你点击某个联系人的某个属性的时候会返回app），此方法只要是默认值，无论你代理方法实现与否都会返回app。
    //         //        _abPeoplePickerVc.predicateForSelectionOfProperty = [NSPredicate predicateWithValue:false];
    //
    //                 //predicateForEnablingPerson默认是true，当设置为false时，所有的联系人都不能被点击。
    //         //        _abPeoplePickerVc.predicateForEnablingPerson = [NSPredicate predicateWithValue:true];
    //             }
    [self presentViewController:_abPeoplePickerVc animated:YES completion:nil];
    
}
#pragma mark - <ABPeoplePickerNavigationControllerDelegate>
// 当用户选中某一个联系人时会执行该方法,并且选中联系人后会直接退出控制器
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    // 1.获取选中联系人的姓名
    CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    //CFStringRef abFullName = ABRecordCopyCompositeName(person);
    // (__bridge NSString *) : 将对象交给Foundation框架的引用来使用,但是内存不交给它来管理
    // (__bridge_transfer NSString *) : 将对象所有权直接交给Foundation框架的应用,并且内存也交给它来管理
    NSString *lastname = (__bridge_transfer NSString *)(lastName);
    NSString *firstname = (__bridge_transfer NSString *)(firstName);
    
    NSLog(@"%@ %@", lastname, firstname);
    //_username = lastname;
    if (lastname==nil&&firstName!=nil)
    {
        
        _model.xingming = [NSString stringWithFormat:@"%@",firstName];
    }
    else if (firstName==nil&&lastname!=nil) {
        
        _model.xingming = [NSString stringWithFormat:@"%@",lastName];
    }
    else
    {
        _model.xingming = [NSString stringWithFormat:@"%@%@",lastName,firstName];
    }
   // _model.xingming = [NSString stringWithFormat:@"%@%@",lastName,firstName];
    // 2.获取选中联系人的电话号码
    // 2.1.获取所有的电话号码
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFIndex phoneCount = ABMultiValueGetCount(phones);
    
     NSMutableArray *tmparray = [NSMutableArray new];
    // 2.2.遍历拿到每一个电话号码
    for (int i = 0; i < phoneCount; i++) {
        if (phoneCount<=2) {
            // 2.2.1.获取电话对应的key
            NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
            
            // 2.2.2.获取电话号码
            NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
            
            NSLog(@"phoneLabel:%@ ---- phoneValue:%@", phoneLabel, phoneValue);
            _model.fangshi = phoneValue;
            
            [tmparray addObject:phoneValue];
        }
    }
    //TODO：获取电话号码要做的事情
    
    _isupdatelayout = YES;
    changeheight = 150;
//    [self.threeview reloadviewframe];
    [self.seletedPhoneArry removeAllObjects];
    
    self.seletedPhoneArry = [NSMutableArray arrayWithArray:tmparray];
    
    [self.threeview reloadviewwitharray:self.seletedPhoneArry];
    [self.tabView reloadData];
    self.pphoneArray = [NSMutableArray arrayWithArray:self.seletedPhoneArry];

}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:NO];
//    [self.view endEditing:YES];
//    
//}


//#pragma mark - 改变tableview的位置（当将要编辑的时候）
//-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//    
//    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textField.tag inSection:0];
//    
//    //将要编辑的textfield 所在的cell  使其滚动到中间的位置
//    
//    [_tabView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
//    
//    return YES;
//}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
